<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>迭代器模式</h1>
      <p>1.意义就是对外提供统一的接口,外部不需要知道内部的数据结构及实现原理</p>
    </div>
    <script>
      const todas = {
        life: ["吃饭", "睡觉", "打豆豆"],
        learn: ["语文", "数学", "外语"],
        work: ["喝茶"],

        each: function (callback) {
          const all = [].concat(this.life, this.learn, this.work);
          for (const item of all) {
            callback(item);
          }
        },

        //使用迭代器的方式
        [Symbol.iterator]: function () {
          const all = [...this.life, ...this.learn, ...this.work];
          let index = 0;
          return {
            next: function () {
              return {
                value: all[index],
                done: index++ >= all.length,
              };
            },
          };
        },
      };

      todas.each(function (item) {
        console.log(item);
      });

      console.log("========================");

      for (const item of todas) {
        console.log(item);
      }
    </script>
  </body>
</html>
